package com.mib.browser;

import com.ireasoning.protocol.snmp.*;
import com.ireasoning.util.MibParseException;
import com.ireasoning.util.MibTreeNode;
import javax.swing.*;
import javax.swing.event.MouseInputListener;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.tree.DefaultMutableTreeNode;
import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.*;
import java.io.File;

public class MIBBrowserFrame extends JFrame implements ActionListener, TreeSelectionListener {
    public static JFileChooser fileChooser = new JFileChooser();
    private MIBBrowserSettings settings;
    private PropertyTableModel propertyTableModel;
    private OperationTableModel operationTableModel;
    private StatusBar statusBar;
    private AboutDialog aboutDialog;
    private SettingsDialog settingsDialog;
    private SetOperationDialog setOperationDialog;
    private DefaultComboBoxModel oidComboModel;
    private DefaultComboBoxModel ipComboModel;
    private SNMPManager snmpManager;
    private JMenuItem aboutMenuItem;
    private JButton clearButton;
    private JMenuItem exitMenuItem;
    private JButton goButton;
    private JButton stopButton;
    private JMenu helpMenu;
    private JSplitPane horizontalSplitPane;
    private JComboBox ipComboBox;
    private JLabel jLabel1;
    private JLabel jLabel2;
    private JLabel jLabel3;
    private JMenuBar jMenuBar1;
    private JScrollPane jScrollPane1;
    private JScrollPane jScrollPane2;
    private JScrollPane jScrollPane3;
    private JMenuItem loadMIBMenuItem;
    private JMenu mibMenu;
    private JTree mibTree;
    private JComboBox oidComboBox;
    private JComboBox operatioComboBox;
    private JTable operationTable;
    private JTable propertyTable;
    private JButton settingsButton;
    private JPanel toolbarPanel;
//    private JMenuItem unloadMIBMenuItem;
    private JSplitPane verticalSplitPane;
    private int[] checkRowColumn;

    public MIBBrowserFrame() {
        this.initComponents();
        this.setTitle("Mib浏览器");
        this.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent evt) {
                MIBBrowserFrame.this.exitApplication();
            }
        });

        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            SwingUtilities.updateComponentTreeUI(this);
            SwingUtilities.updateComponentTreeUI(fileChooser);
            this.pack();
        } catch (Exception var3) {
            var3.printStackTrace();
        }

        Image img = Toolkit.getDefaultToolkit().getImage("res/icon.gif");
        this.setIconImage(img);
        this.loadMIBMenuItem.addActionListener(this);
        this.aboutMenuItem.addActionListener(this);
//        this.unloadMIBMenuItem.addActionListener(this);
        this.exitMenuItem.addActionListener(this);
        this.aboutDialog = new AboutDialog(this, true);
        this.settingsDialog = new SettingsDialog(this, true);
        this.setOperationDialog = new SetOperationDialog(this, true);
        this.mibTree.addTreeSelectionListener(this);

        // 下部显示值表格
        this.operationTableModel = new OperationTableModel();
        this.operationTable.setModel(this.operationTableModel);
        this.operationTable.setDefaultRenderer(String.class, new DefaultTableCellRenderer());
        this.operationTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
        this.operationTable.getColumnModel().getColumn(0).setPreferredWidth(100);
        this.operationTable.getColumnModel().getColumn(1).setPreferredWidth(800);
        // 右键菜单
        JPopupMenu jPopupMenu = new JPopupMenu();
        JMenuItem copy_1 = new JMenuItem("复制");
        copy_1.addActionListener((ActionEvent evt) -> {
            if(this.checkRowColumn.length == 2){
                String obj = this.operationTable.getValueAt(this.checkRowColumn[0], this.checkRowColumn[1]).toString();
                Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
                StringSelection strSel = new StringSelection(obj);
                cb.setContents(strSel, null);
            }
        });
        jPopupMenu.add(copy_1);
        MouseInputListener mouseInputListener = getMouseInputListener(this.operationTable, jPopupMenu);
        this.operationTable.addMouseListener(mouseInputListener);
        this.operationTable.addMouseMotionListener(mouseInputListener);

        //拿到系统剪切板
        operationTable.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_C, KeyEvent.CTRL_DOWN_MASK, true), "CTRL C");
        operationTable.getActionMap().put("CTRL C", new AbstractAction(){
            @Override
            public void actionPerformed(ActionEvent arg0) {
                StringBuilder selection = new StringBuilder();
                for (int row : operationTable.getSelectedRows()) {
                    for (int col : operationTable.getSelectedColumns()) {
                        selection.append(operationTable.getValueAt(row, col)).append("\t");
                    }
                    selection.append("\n");
                }
                Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new StringSelection(selection.toString().trim()), null);
            }
        });


        // 头部详情表格
        this.propertyTableModel = new PropertyTableModel();
        this.propertyTable.setModel(this.propertyTableModel);
        this.propertyTable.setDefaultRenderer(String.class, new DefaultTableCellRenderer());
        this.propertyTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
        this.propertyTable.getColumnModel().getColumn(0).setPreferredWidth(100);
        this.propertyTable.getColumnModel().getColumn(1).setPreferredWidth(800);
        // 右键菜单
        JPopupMenu jPopupMenuHeader = new JPopupMenu();
        JMenuItem copy_2 = new JMenuItem("复制");
        copy_2.addActionListener((ActionEvent evt) -> {
            if(this.checkRowColumn.length == 2){
                String obj = this.propertyTable.getValueAt(this.checkRowColumn[0], this.checkRowColumn[1]).toString();
                Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
                StringSelection strSel = new StringSelection(obj);
                cb.setContents(strSel, null);
            }
        });
        jPopupMenuHeader.add(copy_2);
        MouseInputListener mouseInputHeaderListener = getMouseInputListener(this.propertyTable, jPopupMenuHeader);
        this.propertyTable.addMouseListener(mouseInputHeaderListener);
        this.propertyTable.addMouseMotionListener(mouseInputHeaderListener);

        this.oidComboModel = (DefaultComboBoxModel)this.oidComboBox.getModel();
        this.oidComboModel.setSelectedItem("");
        this.ipComboModel = (DefaultComboBoxModel)this.ipComboBox.getModel();
        this.ipComboModel.setSelectedItem("127.0.0.1");
        this.settings = new MIBBrowserSettings();
        this.loadSettings();
        this.addComboModelElements();

        // 加载mib
        File directory = new File("mibs");
        File[] files = directory.listFiles();
        if(files != null){
            for(File file : files){
                this.loadMibFile("mibs/" + file.getName(), false);
            }
        }

        this.snmpManager = new SNMPManager(this, this.goButton, this.oidComboBox, this.operationTableModel);

        // 显示宽度
        this.show();
        Toolkit kit = Toolkit.getDefaultToolkit();
        Dimension screenSize = kit.getScreenSize();
        int screenWidth = screenSize.width;
        int screenHeight = screenSize.height;
        int width = screenWidth - 200;
        int height = screenHeight - 150;
        this.setSize(width, height);
        this.setLocation(100, 75);
    }

    public int getPreferredRowHeight(JTable table, int rowIndex, int margin) {
        int height = table.getRowHeight();

        for(int c = 0; c < table.getColumnCount(); ++c) {
            TableCellRenderer renderer = table.getCellRenderer(rowIndex, c);
            Component comp = table.prepareRenderer(renderer, rowIndex, c);
            int h = comp.getPreferredSize().height + 2 * margin;
            height = Math.max(height, h);
        }

        return height;
    }

    public void packRows(JTable table, int margin) {
        this.packRows(table, 0, table.getRowCount(), margin);
    }

    public void packRows(JTable table, int start, int end, int margin) {
        for(int r = 0; r < table.getRowCount(); ++r) {
            int h = this.getPreferredRowHeight(table, r, margin);
            if (table.getRowHeight(r) != h) {
                table.setRowHeight(r, h);
            }
        }

    }

    private void addComboModelElements() {
        String[] oids = this.settings.getOIDs();
        String[] ips = this.settings.getIPs();

        int i;
        for(i = 0; i < oids.length; ++i) {
            if (!oids[i].equals("")) {
                this.oidComboModel.addElement(oids[i]);
            }
        }

        for(i = 0; i < ips.length; ++i) {
            if (!ips[i].equals("")) {
                this.ipComboModel.addElement(ips[i]);
            }
        }

    }

    private void saveSettigs() {
        int pos = this.verticalSplitPane.getDividerLocation();
        this.settings.setVericalDividerLocation(pos);
        pos = this.horizontalSplitPane.getDividerLocation();
        this.settings.setHorizontalDividerLocation(pos);
        this.settings.setFrameX(this.getX());
        this.settings.setFrameY(this.getY());
        this.settings.setFrameWidth(this.getWidth());
        this.settings.setFrameHeight(this.getHeight());
        this.settings.setIPs(this.ipComboModel);
        this.settings.setOIDs(this.oidComboModel);
        this.settings.save();
    }

    private void loadSettings() {
        this.settings.load();
        this.verticalSplitPane.setDividerLocation(this.settings.getVericalDividerLocation());
        this.horizontalSplitPane.setDividerLocation(this.settings.getHorizontalDividerLocation());
        this.setBounds(this.settings.getFrameX(), this.settings.getFrameY(), this.settings.getFrameWidth(), this.settings.getFrameHeight());
    }

    public void valueChanged(TreeSelectionEvent e) {
        DefaultMutableTreeNode node = (DefaultMutableTreeNode)e.getPath().getLastPathComponent();
        if (node.getUserObject() instanceof MIBBrowserTree.MIBBrowserTreeNode) {
            MIBBrowserTree.MIBBrowserTreeNode n = (MIBBrowserTree.MIBBrowserTreeNode)node.getUserObject();
            MibTreeNode mib = n.getMibTreeNode();

            if (mib.isGroupNode()) {
                this.oidComboModel.setSelectedItem(n.getMibTreeNode().getOID().toString());
            } else {
                this.oidComboModel.setSelectedItem(n.getMibTreeNode().getOID().toString() + ".0");
            }

            if(this.operatioComboBox.getSelectedIndex() != 3){
                this.goButton.setEnabled(true);
            }
            this.propertyTableModel.setSelectedMibTreeNode(n.getMibTreeNode());
        }
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource().equals(this.loadMIBMenuItem)) {
            int returnVal = fileChooser.showOpenDialog(this);
            if (returnVal == 0) {
                File selectedFile = fileChooser.getSelectedFile();
                String fileName = selectedFile.getAbsolutePath();
                this.loadMibFile(fileName, true);
            }
        } else if (e.getSource().equals(this.aboutMenuItem)) {
            Rectangle rec = this.getBounds();
            int w = this.aboutDialog.getWidth();
            int h = this.aboutDialog.getHeight();
            int x = rec.x + (rec.width - w) / 2;
            int y = rec.y + (rec.height - h) / 2;
            this.aboutDialog.setLocation(x, y);
            this.aboutDialog.setVisible(true);
        }
//        else if (e.getSource().equals(this.unloadMIBMenuItem)) {
//            ((MIBBrowserTree)this.mibTree).unloadSelectedFile();
//        }
        else if (e.getSource().equals(this.exitMenuItem)) {
            this.exitApplication();
        }
    }

    private void loadMibFile(String fileName, boolean isMsg) {
        this.statusBar.setMessage("加载" + fileName + " ...");
        MibTreeNode node = null;

        try {
            node = MibUtil.parseMib(fileName, false);
        } catch (Exception var5) {
            var5.printStackTrace();
            return;
        }

        ((MIBBrowserTree)this.mibTree).openMIBFile(node, true);
        this.statusBar.setMessage("默认mib加载完成...");
        if(isMsg){
            JOptionPane.showMessageDialog(null, "导入mib【" + fileName + "】成功！");
        }
    }

    private void exitApplication() {
        this.saveSettigs();
        System.exit(0);
    }

    public String getSelectedOID() {
        return (String)this.oidComboBox.getSelectedItem();
    }

    public void setStatusBarMessage(String message) {
        this.statusBar.setMessage(message);
    }

    private void initComponents() {
        this.toolbarPanel = new JPanel();
        this.jLabel1 = new JLabel();
        this.ipComboBox = new JComboBox();
        this.settingsButton = new JButton();
        this.jLabel2 = new JLabel();
        this.oidComboBox = new JComboBox();
        this.jLabel3 = new JLabel();
        this.operatioComboBox = new JComboBox();
        this.goButton = new JButton();
        this.stopButton = new JButton();
        this.clearButton = new JButton();
        this.verticalSplitPane = new JSplitPane();
        this.jScrollPane1 = new JScrollPane();
        this.mibTree = new MIBBrowserTree();
        this.horizontalSplitPane = new JSplitPane();
        this.jScrollPane2 = new JScrollPane();
        this.propertyTable = new JTable();
        this.jScrollPane3 = new JScrollPane();
        this.operationTable = new JTable();
        this.statusBar = new StatusBar();
        this.jMenuBar1 = new JMenuBar();
        this.mibMenu = new JMenu();
        this.loadMIBMenuItem = new JMenuItem();
//        this.unloadMIBMenuItem = new JMenuItem();
        this.exitMenuItem = new JMenuItem();
        this.helpMenu = new JMenu();
        this.aboutMenuItem = new JMenuItem();
        this.setDefaultCloseOperation(3);
        this.jLabel1.setText("IP地址:");
        this.ipComboBox.setEditable(true);
        this.settingsButton.setText("配置");
        this.settingsButton.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent evt) {
                MIBBrowserFrame.this.settingsButtonMouseClicked(evt);
            }
        });
        this.jLabel2.setText("OID:");
        this.oidComboBox.setEditable(true);
        this.jLabel3.setText("操作:");
        this.operatioComboBox.setModel(new DefaultComboBoxModel(new String[]{"当前值", "下一个值", "连续下一个值", "Walk", "设置值"}));

        this.goButton.setText("查询");
        this.goButton.setEnabled(false);
        this.goButton.addActionListener(MIBBrowserFrame.this::goButtonMouseClicked);

        this.stopButton.setText("停止");
        this.stopButton.setEnabled(false);
        this.stopButton.addActionListener(MIBBrowserFrame.this::stopButtonMouseClicked);

        this.clearButton.setText("清除");
        this.clearButton.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent evt) {
                MIBBrowserFrame.this.clearButtonMouseClicked(evt);
            }
        });

        GroupLayout toolbarPanelLayout = new GroupLayout(this.toolbarPanel);
        this.toolbarPanel.setLayout(toolbarPanelLayout);
        toolbarPanelLayout.setHorizontalGroup(
                toolbarPanelLayout.createParallelGroup(GroupLayout.Alignment.CENTER)
                        .addGroup(toolbarPanelLayout.createSequentialGroup().addContainerGap()
                                .addComponent(this.jLabel1).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(this.ipComboBox, -2, 157, -2).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(this.settingsButton, -1, 111, 32767)
                                .addGap(22, 22, 22)
                                .addComponent(this.jLabel2).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(this.oidComboBox, 0, 231, 32767)
                                .addGap(25, 25, 25)
                                .addComponent(this.jLabel3).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(this.operatioComboBox, 0, 60, 32767).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(this.goButton, -2, 65, -2).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(this.stopButton, -2, 65, -2).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(this.clearButton, -2, 68, -2).addContainerGap()
                        )
        );
        toolbarPanelLayout.setVerticalGroup(
                toolbarPanelLayout.createParallelGroup(GroupLayout.Alignment.CENTER)
                        .addGroup(toolbarPanelLayout.createSequentialGroup().addContainerGap().
                                addGroup(toolbarPanelLayout.createParallelGroup(GroupLayout.Alignment.CENTER)
                                        .addComponent(this.jLabel1)
                                        .addComponent(this.ipComboBox, -2, -1, -2)
                                        .addComponent(this.settingsButton)
                                        .addComponent(this.jLabel2)
                                        .addComponent(this.oidComboBox, -2, -1, -2)
                                        .addComponent(this.jLabel3)
                                        .addComponent(this.operatioComboBox, -2, -1, -2)
                                        .addComponent(this.goButton)
                                        .addComponent(this.stopButton)
                                        .addComponent(this.clearButton)
                                ).addContainerGap(-1, 32767)
                        )
        );
        this.jScrollPane1.setViewportView(this.mibTree);
        this.verticalSplitPane.setLeftComponent(this.jScrollPane1);
        this.horizontalSplitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
        this.propertyTable.setModel(new DefaultTableModel(new Object[][]{{null, null, null, null}, {null, null, null, null}, {null, null, null, null}, {null, null, null, null}}, new String[]{"Title 1", "Title 2", "Title 3", "Title 4"}));
        this.jScrollPane2.setViewportView(this.propertyTable);
        this.horizontalSplitPane.setTopComponent(this.jScrollPane2);
        this.operationTable.setModel(new DefaultTableModel(new Object[][]{{null, null, null, null}, {null, null, null, null}, {null, null, null, null}, {null, null, null, null}}, new String[]{"Title 1", "Title 2", "Title 3", "Title 4"}));
        this.jScrollPane3.setViewportView(this.operationTable);
        this.horizontalSplitPane.setRightComponent(this.jScrollPane3);
        this.verticalSplitPane.setRightComponent(this.horizontalSplitPane);

        this.mibMenu.setText("Mib文件");
        this.loadMIBMenuItem.setText("导入Mib文件");
        this.mibMenu.add(this.loadMIBMenuItem);
//        this.unloadMIBMenuItem.setText("卸载Mib文件");
//        this.mibMenu.add(this.unloadMIBMenuItem);
        this.exitMenuItem.setText("退出");
        this.mibMenu.add(this.exitMenuItem);
        this.jMenuBar1.add(this.mibMenu);
        this.helpMenu.setText("帮助");
        this.aboutMenuItem.setText("关于");
        this.helpMenu.add(this.aboutMenuItem);
        this.jMenuBar1.add(this.helpMenu);
        this.setJMenuBar(this.jMenuBar1);
        GroupLayout layout = new GroupLayout(this.getContentPane());
        this.getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(GroupLayout.Alignment.CENTER)
                        .addComponent(this.toolbarPanel, -1, -1, 32767)
                        .addGroup(layout.createSequentialGroup().addContainerGap()
                                .addComponent(this.statusBar, -1, -1, 32767).addContainerGap())
                        .addGroup(layout.createSequentialGroup().addContainerGap()
                                .addComponent(this.verticalSplitPane, -1, 1135, 32767).addContainerGap()
                        )
        );
        layout.setVerticalGroup(
                layout.createParallelGroup(GroupLayout.Alignment.CENTER)
                        .addGroup(layout.createSequentialGroup()
                                .addComponent(this.toolbarPanel, -2, -1, -2).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(this.verticalSplitPane, -1, 685, 32767).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(this.statusBar, -2, -1, -2)
                        )
        );
        this.pack();
    }

    private void stopButtonMouseClicked(ActionEvent evt) {
        this.goButton.setEnabled(true);
        this.stopButton.setEnabled(false);
        this.snmpManager.stopSnmpWalk();
    }

    private void goButtonMouseClicked(ActionEvent evt) {
        DefaultMutableTreeNode node = (DefaultMutableTreeNode)this.mibTree.getSelectionPath().getLastPathComponent();
        if (node.getUserObject() instanceof MIBBrowserTree.MIBBrowserTreeNode) {
            MIBBrowserTree.MIBBrowserTreeNode n = (MIBBrowserTree.MIBBrowserTreeNode)node.getUserObject();
            MibTreeNode mib = n.getMibTreeNode();
            switch (this.operatioComboBox.getSelectedIndex()) {
                case 0:
                    if (mib.isGroupNode()) {
                        JOptionPane.showMessageDialog(this, " 节点上的操作无效！", "错误", 0);
                        return;
                    }
                    this.snmpManager.setHost((String)this.ipComboBox.getSelectedItem());
                    this.snmpManager.setOid((String)this.oidComboBox.getSelectedItem());
                    this.snmpManager.snmpGet();
                    break;
                case 1:
                    this.snmpManager.setHost((String)this.ipComboBox.getSelectedItem());
                    this.snmpManager.setOid((String)this.oidComboBox.getSelectedItem());
                    this.snmpManager.snmpGetNext();
                    break;
                case 2:
                    this.snmpManager.setHost((String)this.ipComboBox.getSelectedItem());
                    this.snmpManager.setOid((String)this.oidComboBox.getSelectedItem());
                    String lastOid = this.snmpManager.snmpGetNext();
                    if(lastOid != null){
                        this.oidComboBox.setSelectedItem(lastOid);
                    }
                    break;
                case 3:
                    this.goButton.setEnabled(false);
                    this.stopButton.setEnabled(true);
                    this.snmpManager.setHost((String)this.ipComboBox.getSelectedItem());
                    this.snmpManager.setOid((String)this.oidComboBox.getSelectedItem());
                    this.snmpManager.snmpWalk();
                    break;
                case 4:
                    Rectangle rec = this.getBounds();
                    int w = this.setOperationDialog.getWidth();
                    int h = this.setOperationDialog.getHeight();
                    int x = rec.x + (rec.width - w) / 2;
                    int y = rec.y + (rec.height - h) / 2;
                    this.setOperationDialog.setLocation(x, y);
                    this.setOperationDialog.setOID((String)this.oidComboBox.getSelectedItem());
                    this.setOperationDialog.setVisible(true);
                    if (mib.isGroupNode()) {
                        JOptionPane.showMessageDialog(this, "节点上的操作无效", "错误", JOptionPane.ERROR_MESSAGE);
                        return;
                    }
                    break;
            }
            if (this.ipComboModel.getIndexOf(this.ipComboBox.getSelectedItem()) < 0) {
                this.ipComboModel.addElement(this.ipComboBox.getSelectedItem());
            }
//            if (this.oidComboModel.getIndexOf(this.oidComboBox.getSelectedItem()) < 0) {
//                this.oidComboModel.addElement(this.oidComboBox.getSelectedItem());
//            }
        }
    }

    public void applySettings() {
        int [] snmpVersion = {0, 1, 3};
        try {
            this.snmpManager.setPort(this.settingsDialog.getPort());
        }catch (Exception e){
            JOptionPane.showMessageDialog(new JFrame(), "检查SNMP设置对话框中的端口字段,只能为数字！", "警告", JOptionPane.ERROR_MESSAGE);
        }
        this.snmpManager.setReadCommunity(this.settingsDialog.getReadCommunity());
        this.snmpManager.setWriteCommunity(this.settingsDialog.getWriteCommunity());
        this.snmpManager.setVersion(snmpVersion[this.settingsDialog.getSNMPVersion()]);
        if (this.snmpManager.getVersion() == 3) {
            this.snmpManager.setUser(this.settingsDialog.getUSMUser());
            this.snmpManager.setSecurityLevel(this.settingsDialog.getSecurityLevel());
            this.snmpManager.setAuthProtocol(this.settingsDialog.getAuthAlgorithm());
            this.snmpManager.setAuthPassword(this.settingsDialog.getAuthPassword());
            this.snmpManager.setPrivProtocol(this.settingsDialog.getPrivacyAlgorithm());
            this.snmpManager.setPrivPassword(this.settingsDialog.getPrivacyPassword());
        }

    }

    public void applySetOperation() {
        this.snmpManager.setHost((String)this.ipComboBox.getSelectedItem());
        this.snmpManager.setOid((String)this.oidComboBox.getSelectedItem());
        SnmpOID o = MibUtil.lookupOID((String)this.oidComboBox.getSelectedItem());
        SnmpDataType t = this.translate();
        SnmpVarBind vb = new SnmpVarBind(o, t);
        this.snmpManager.snmpSet(vb);
    }

    public SnmpDataType translate() {
        String value = this.setOperationDialog.getValue();
        switch (this.setOperationDialog.getSelectedDataTypeIndex()) {
            case 0:
                return new SnmpOctetString(value);
            case 1:
                return new SnmpInt(Integer.parseInt(value));
            case 2:
                return new SnmpOID(value);
            case 3:
                return new SnmpGauge32(value);
            case 4:
                return new SnmpCounter32(value);
            case 5:
                return new SnmpIpAddress(value);
            case 6:
                return new SnmpTimeTicks(Long.parseLong(value));
            case 7:
                return new SnmpCounter64(Long.parseLong(value));
            case 8:
                return new SnmpUInt(Long.parseLong(value));
            case 9:
                SnmpBits bits = new SnmpBits(16);
                bits.setValue(value);
            default:
                throw new RuntimeException("Unknown data type");
        }
    }

    private void clearButtonMouseClicked(MouseEvent evt) {
        this.operationTableModel.clearAll();
    }

    private void settingsButtonMouseClicked(MouseEvent evt) {
        Rectangle rec = this.getBounds();
        int w = this.settingsDialog.getWidth();
        int h = this.settingsDialog.getHeight();
        int x = rec.x + (rec.width - w) / 2;
        int y = rec.y + (rec.height - h) / 2;
        this.settingsDialog.setLocation(x, y);
        this.settingsDialog.setAddress((String)this.ipComboBox.getSelectedItem());
        this.settingsDialog.setVisible(true);
    }

    private MouseInputListener getMouseInputListener(JTable jTable, JPopupMenu popupMenu){
        return new MouseInputListener() {
            public void mouseClicked(MouseEvent e) {
                processEvent(e);
            }
            public void mousePressed(MouseEvent e) {
                processEvent(e);
            }
            public void mouseReleased(MouseEvent e) {
                processEvent(e);
                if ((e.getModifiers() & MouseEvent.BUTTON3_MASK) != 0 && !e.isControlDown() && !e.isShiftDown()) {
                    Point mousepoint;
                    mousepoint = e.getPoint();
                    int row = jTable.rowAtPoint(mousepoint);
                    int col = jTable.columnAtPoint(mousepoint);
                    checkRowColumn = new int[]{row, col};
                    popupMenu.show(jTable, e.getX(), e.getY());
                }
            }
            public void mouseEntered(MouseEvent e) {
                processEvent(e);
            }
            public void mouseExited(MouseEvent e) {
                processEvent(e);
            }
            public void mouseDragged(MouseEvent e) {
                processEvent(e);
            }
            public void mouseMoved(MouseEvent e) {
                processEvent(e);
            }
            private void processEvent(MouseEvent e) {
                if ((e.getModifiers() & MouseEvent.BUTTON3_MASK) != 0) {
                }
            }
        };
    }
}
